require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'dialog',
    'model',
    'datepicker'
], function ($, common, ready, Collection, template, dialog, Model, datepicker) {
    var goods_id = getUrlPath(4);
    var site_id = getUrlPath(3);
    var id = getUrlPath(5);
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            this.fetch();
            this.event();
        },

        fetch: function () {
            $.ajax({
                url: _global.url.api + 'make_card',
                data: {goods_id: goods_id, site_id: site_id, id: id},
                type: 'get',
                async: true,
                success: function (res) {
                    console.log("res ,", res);
                    if (res.code == 0) {
                        var html = template('data-item', res.data);
                        $("#data-list").html(html);
                    }
                }
            });

        },

        event: function () {
            var _this = this;
            //日期
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd'
                });
            });

            $('body').on('click', '[name="tag_limit"]', function (e) {
                var tag_limit = $('[name="tag_limit"]:checked').val();
                if (tag_limit == 1) {
                    $('.card_user_tag').show();
                } else {
                    $('.card_user_tag').hide();
                }
            });
            //保存
            $('.btn-save').on('click', function (e) {
                _this.data = {};
                _this.data.type = 1;
                _this.data.title = $('[name=title]').val();
                _this.data.id = $('[name=id]').val();
                _this.data.card_id = $('[name=card_id]').val();
                if (_this.data.title == '') {
                    Alert({msg: '请输入标题', type: 'error'});
                    return false;
                }
                _this.data.intro = $('[name=intro]').val();
                if (_this.data.intro == '') {
                    Alert({msg: '请输入备注', type: 'error'});
                    return false;
                }
                _this.data.corner_ico = $.trim($('[name="corner_ico"]:checked').val());
                _this.data.tag_limit = $('[name="tag_limit"]:checked').val();
                var user_tag_id_arr = new Array();
                $('[name="user_tag_id"]:checked').each(function (i) {
                    var user_tag_id = $.trim($(this).val());
                    user_tag_id_arr.push(user_tag_id);
                });
                var card_cate = new Array();
                $('[name="card_cate"]:checked').each(function (i) {
                    var card_cate_id = $.trim($(this).val());
                    card_cate.push(card_cate_id);
                });
                if (_this.data.tag_limit == 1) {
                    if (user_tag_id_arr.length < 1) {
                        Alert({msg: '请选择用户标签', type: 'error'});
                        return false;
                    }
                }
                _this.data.user_tag_id = user_tag_id_arr;
                _this.data.card_cate = card_cate;

                _this.data.url = $('[name=url]').val();
                _this.data.content = $.trim($('[name=content]').val());
                _this.data.image = $.trim($('[name=image]').val());
                _this.data.width = $.trim($('[name=width]').val());
                _this.data.height = $.trim($('[name=height]').val());
                if (_this.data.width == '') {
                    Alert({msg: '请输入宽度', type: 'error'});
                    return false;
                }
                if (_this.data.height == '') {
                    Alert({msg: '请输入高度', type: 'error'});
                    return false;
                }
                if (_this.data.image == '') {
                    Alert({msg: '请上传图片', type: 'error'});
                    return false;
                }
                if (new Model({
                        url: _global.url.api + 'make_card',
                        data: _this.data
                    }).post()) {
                    _this.collection.reload();
                }
            });
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                        location.href = '/admin/promotion_sign_up';
                    }
                });
                _this.upload_dialog.show();
            });
        },
        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                $('#img-view').html(template('img-tpl', img));
            }
        },
    };

    new index();

});